package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import com.sky.utils.CurrentHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    private static final Logger log = LoggerFactory.getLogger(EmployeeServiceImpl.class);
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }


    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     */
    @Override
    public void editPassword(String oldPassword, String newPassword) {
        Long empId = CurrentHolder.getCurrentId();
        Employee employee = employeeMapper.getById(empId);
        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (employee.getPassword().equals(oldPassword) && employee.getPassword()!=null){
            employeeMapper.updatePassword(empId,newPassword);
        }else {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
    }

    /**
     * 根据id查询员工
     * @param id
     * @return
     */
    @Override
    public Employee getById(Long id) {
      Employee employee =  employeeMapper.getById(id);
        return employee;
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageResult page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Employee> employees = employeeMapper.getEmpList(name);
        Page<Employee> pageInfo = (Page<Employee>) employees;
        return new PageResult(pageInfo.getTotal(),pageInfo.getResult());
    }

    /**
     * 新增员工
     * @param employeeDTO
     */
    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        employee.setStatus(1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(CurrentHolder.getCurrentId());
        employee.setUpdateUser(CurrentHolder.getCurrentId());
        employee.setPassword("123456");
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.save( employee);

    }

    /**
     * 修改员工
     * @param employeeDTO
     */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(CurrentHolder.getCurrentId());
        BeanUtils.copyProperties(employeeDTO,employee);
        employeeMapper.update(employee);

    }

    /**
     * 修改员工状态
     * @param status
     * @param id
     */
    @Override
    public void setStatus(String status, String id) {
        //设置更新时间和更新者
        Employee employee = new Employee();
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(CurrentHolder.getCurrentId());
        Integer status1 = Integer.valueOf(status);
        Long id1 = Long.valueOf(id);
        employee.setStatus(status1);
        employee.setId(id1);
        employeeMapper.serStatus(employee);

    }

}
